﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="StudyEditViewModel.cs" company="ELEN 7045 GROUP II">>
//   
// </copyright>
// <summary>
//   The Studies View Model.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Presentation.Module.StudyDefinition.ViewModels
{
    #region

    using System.ComponentModel;
    using System.Windows.Input;

    using Microsoft.Practices.Prism.Commands;
    using Microsoft.Practices.Prism.Regions;

    using Presentation.Module.StudyDefinition.Interfaces.Services;
    using Presentation.Module.StudyDefinition.Interfaces.ViewModels;
    using Presentation.Module.StudyDefinition.Interfaces.Views;
    using Presentation.Module.StudyDefinition.Models;

    #endregion

    /// <summary>
    /// StudyEditViewModel Implementation
    /// </summary>
    public class StudyEditViewModel : StudyDefinitionViewModelBase, IStudyEditViewModel, INotifyPropertyChanged
    {
        #region Constants and Fields

        /// <summary>
        /// A study item
        /// </summary>
        private Study currentItem;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="StudyEditViewModel"/> class.
        /// </summary>
        /// <param name="regionManager">
        /// The region manager.
        /// </param>
        /// <param name="studiesService">
        /// The studies service.
        /// </param>
        public StudyEditViewModel(IRegionManager regionManager, IStudiesUIProxyService studiesService)
            : base(regionManager, studiesService)
        {
            /* ... Relies on abstract base class for dependencies. */

            // Setting up command delegates 
            this.EditStudy = new DelegateCommand<Study>(this.OnEditStudy);
            
        }

        #endregion

        #region Events

        /// <summary>
        /// Occurs when a property value changes.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets CurrentItem.
        /// </summary>
        public Study CurrentItem
        {
            get
            {
                return this.currentItem;
            }

            set
            {
                if (value == null)
                {
                    return;
                }

                this.currentItem = value;
                this.RaiseOnPropertyChanged("CurrentItem");
            }
        }

        /// <summary>
        /// Gets the Edit study command.
        /// </summary>
        public ICommand EditStudy { get; private set; }

        /// <summary>
        /// Gets or sets the view.
        /// </summary>
        /// <value>
        /// The study Edit view.
        /// </value>
        public IStudyEditView View { get; set; }

        #endregion

        #region Implemented Interfaces

        #region IStudyEditViewModel

        /// <summary>
        /// The initialize.
        /// </summary>
        public void Initialize()
        {
            // Populate the current item to for use by the view.
            this.currentItem = new Study();
        }

        /// <summary>
        /// Called when [Edit study].
        /// </summary>
        /// <param name="study">
        /// The study.
        /// </param>
        public void OnEditStudy(Study study)
        {
            this.StudiesServiceInstance.EditStudy(study);
        }

        #endregion

        #endregion

        #region Methods

        /// <summary>
        /// Raises the on property changed.
        /// </summary>
        /// <param name="propertyName">
        /// Name of the property.
        /// </param>
        protected void RaiseOnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        /// <summary>
        /// Called when [cancel].
        /// </summary>
        private void OnCancel()
        {
            this.RegionManagerInstance.Regions["MainRegion"].Deactivate(this.View);
        }

        #endregion
    }
}
